<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 5/9/14
 * Time: 3:18 PM
 */
class Post extends Eloquent {

    public static $table = 'post';
    public static $key = 'id';
    public static $timestamps = true;


    function __construct(){
        parent::__construct();
    }

    public function comments()
    {
        return $this->has_many('Comments');
    }

    public static function findPK($id){
        if($id){
            if(Cache::get("post_pk_".$id)){
                return Cache::get("post_pk_".$id);
            }
            $result = DB::table('post')->where('id', '=', $id)->first();
            Cache::put("post_pk_".$id , $result,60*24*30);
            return $result;
        }
    }

    public static function total(){

        if(Cache::get("post_total")){
            return Cache::get("post_total");
        }
        $count = Post::count();
        Cache::put("post_total",$count,60*24*30);
        return $count;
    }

    public function user()
    {
        return $this->belongs_to('Users');
    }

}